﻿namespace EFS.RC.Wallboard.Controls.Common.RSS
{
	/// <summary>
	/// Interaction logic for List.xaml
	/// </summary>
	public partial class List : System.Windows.Controls.UserControl
	{
		#region Locals

		/// all items read
		private System.Collections.Generic.List<System.ServiceModel.Syndication.SyndicationItem> syndicationItemArray =
				new System.Collections.Generic.List<System.ServiceModel.Syndication.SyndicationItem>();

		/// current item index
		private System.Int32 currentItemIndex = -1;

		/// timer to swap items
		private System.Windows.Threading.DispatcherTimer timerSwapItems = null;

		/// timer to swap items animate
		private System.Windows.Threading.DispatcherTimer timerSwapItemsAnimate = null;
		private System.Windows.Controls.Viewbox vieboxShow = null;
		private EFS.RC.Wallboard.Controls.Common.RSS.Item controlItemShow = null;
		private System.Windows.Controls.Viewbox vieboxHide = null;
		private EFS.RC.Wallboard.Controls.Common.RSS.Item controlItemHide = null;
		#endregion

		#region Constructors
		/// Implicit
		public List()
		{
			InitializeComponent();
		}
		#endregion

		#region Properties
		/// Property NextSyndicationItem: Get
		private System.ServiceModel.Syndication.SyndicationItem NextSyndicationItem
		{
			get
			{
				if (-1 == currentItemIndex || currentItemIndex >= syndicationItemArray.Count - 1)
				{
					// get url
					System.Xml.XmlReader reader = System.Xml.XmlReader.Create(EFS.RC.Wallboard.Common.Config.Sections.RSS.Url);
					System.ServiceModel.Syndication.SyndicationFeed feed = System.ServiceModel.Syndication.SyndicationFeed.Load(reader);

					// create array
					syndicationItemArray = new System.Collections.Generic.List<System.ServiceModel.Syndication.SyndicationItem>();
					foreach (System.ServiceModel.Syndication.SyndicationItem item in feed.Items)
					{
						if (syndicationItemArray.Count >= EFS.RC.Wallboard.Common.Config.Sections.RSS.ReadCount)
						{
							break;
						}
						syndicationItemArray.Add(item);
					}
					syndicationItemArray.Sort(new System.Comparison<System.ServiceModel.Syndication.SyndicationItem>((x, y) => System.DateTime.Compare(y.PublishDate.DateTime, x.PublishDate.DateTime)));
					currentItemIndex = 0;
				}
				else
				{
					++currentItemIndex;
				}

				// ok
				return syndicationItemArray[currentItemIndex];
			}
		}
		#endregion

		#region Utilities
		/// Start
		public void Start()
		{
			StartSwapItems();
		}

		/// Stop
		public void Stop()
		{
			StopSwapItems();
		}

			#region Swap
		/// Start swap
		private void StartSwapItems()
		{
			// stop swap
			StopSwapItems();

			timerSwapItems = new System.Windows.Threading.DispatcherTimer();
			timerSwapItems.Interval = new System.TimeSpan(0, 0, 0, 0, EFS.RC.Wallboard.Common.Config.Sections.RSS.DelayMilliseconds);
			timerSwapItems.Tick += new System.EventHandler(timerSwapItems_Tick);
			timerSwapItems.Start();

			SetNextSyndicationItem();
		}

		/// SetNextSyndicationItem()
		private void SetNextSyndicationItem()
		{
			if (viewboxRssItem1.Opacity >= 1)
			{
				this.vieboxShow = viewboxRssItem2;
				this.controlItemShow = controlRssItem2;
				this.vieboxHide = viewboxRssItem1;
				this.controlItemHide = controlRssItem1;
			}
			else
			{
				this.vieboxShow = viewboxRssItem1;
				this.controlItemShow = controlRssItem1;
				this.vieboxHide = viewboxRssItem2;
				this.controlItemHide = controlRssItem2;
			}

			controlItemShow.Data = NextSyndicationItem;

			// start animate
			StartSwapItemsAnimate();
		}

		/// Swap sequence
		private void timerSwapItems_Tick(object sender, System.EventArgs e)
		{
			// get next image
			if (null != timerSwapItems && timerSwapItems.IsEnabled)
			{
				SetNextSyndicationItem();
			}	
		}

		/// Stop swap
		private void StopSwapItems()
		{
			// stop animate
			StopSwapItemsAnimate();

			// stop waiting
			if (null != timerSwapItems && timerSwapItems.IsEnabled)
			{
				timerSwapItems.Stop();
				timerSwapItems = null;
			}
		}
			#endregion

			#region SwapAnimate
		/// Start animate
		private void StartSwapItemsAnimate()
		{
			// stop animate
			StopSwapItemsAnimate();

			timerSwapItemsAnimate = new System.Windows.Threading.DispatcherTimer();
			timerSwapItemsAnimate.Interval = new System.TimeSpan(0, 0, 0, 0, 100);
			timerSwapItemsAnimate.Tick += new System.EventHandler(timerSwapItemsAnimate_Tick);
			timerSwapItemsAnimate.Start();
		}

		/// Animate sequence
		private void timerSwapItemsAnimate_Tick(object sender, System.EventArgs e)
		{
			// get next image
			if (null != timerSwapItemsAnimate && timerSwapItemsAnimate.IsEnabled)
			{
				this.vieboxShow.Opacity += 0.1;
				this.vieboxHide.Opacity -= 0.1;

				// stop condition
				if (this.vieboxShow.Opacity >= 1)
				{
					this.vieboxShow.Opacity = 1;
					this.vieboxHide.Opacity = 0;
					StopSwapItemsAnimate();
				}
			}
		}

		/// Stop animate
		private void StopSwapItemsAnimate()
		{
			// stop waiting
			if (null != timerSwapItemsAnimate && timerSwapItemsAnimate.IsEnabled)
			{
				timerSwapItemsAnimate.Stop();
				timerSwapItemsAnimate = null;
			}
		}
			#endregion
		#endregion
	}

}